//
//  baseTableVC.m
//  jiaKaoZhuShou
//
//  Created by 金艺社 on 2018/5/18.
//  Copyright © 2018年 金艺社. All rights reserved.
//

#import "baseTableVC.h"

@interface baseTableVC () <DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
@property(nonatomic, assign) UITableViewStyle   style;
@end

@implementation baseTableVC
- (instancetype)initWithStyle:(UITableViewStyle)style
{
    self = [super init];
    if (self) {
        self.style = style;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:self.style];
    tableView.delegate = self;
    tableView.dataSource = self;
    self.tableView = tableView;
    [self.view addSubview:self.tableView];
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.view);
        if (kScreenHeight == 812) {
            make.bottom.equalTo(self.view.mas_bottom).offset(-34);
        } else {
            make.bottom.equalTo(self.view.mas_bottom);
        }
    }];
    
    [self confingureTableView];
}

- (void)confingureTableView
{
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"base.cell"];
    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate = self;
    if (@available(iOS 11.0, *)) {
        self.tableView.estimatedSectionHeaderHeight = 0;
        self.tableView.estimatedSectionFooterHeight = 0;
        self.tableView.estimatedRowHeight = 0;
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
}

#pragma mark - refresh
- (void)confingureRefresh
{
    [self addRefreshHeader];
    [self addRefreshFooter];
}

- (void)addRefreshFooter
{
    //    self.tableView.mj_footer = [GKRefreshFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
    footer.stateLabel.textColor = [UIColor grayColor];
    footer.stateLabel.font = [UIFont systemFontOfSize:14];
    [footer setTitle:@"没有更多了" forState:MJRefreshStateNoMoreData];
    self.tableView.mj_footer = footer;
}

- (void)addRefreshHeader
{
    self.tableView.mj_header = [GKRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNew)];
}

- (void)loadNew
{
    
}

- (void)loadMore
{
    
}

- (void)endRefreshing
{
    [self endFooterRefresh];
    [self endHeaderRefresh];
}

- (void)beginHeaderRefreshing
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.tableView.mj_header beginRefreshing];
    });
    
}

- (void)beginFooterRefreshing
{
    [self.tableView.mj_footer beginRefreshing];
}

- (void)endHeaderRefresh
{
    [self.tableView.mj_header endRefreshing];
}

- (void)endFooterRefresh
{
    [self.tableView.mj_footer endRefreshing];
}

- (void)reloadTableWithPageTotal:(NSUInteger)pageTotal
{
    if (self.dataArray.count) {
        self.tableView.mj_footer.hidden = NO;
        if (self.dataArray.count == pageTotal) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        } else {
            [self.tableView.mj_footer resetNoMoreData];
        }
    } else {
        self.tableView.mj_footer.hidden = YES;
    }
    [self.tableView reloadData];
}

- (BOOL)isHeaderRefreshing
{
    return self.tableView.mj_header.isRefreshing;
}

- (BOOL)isFooterRefreshing
{
    return self.tableView.mj_footer.isRefreshing;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 0;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"base.cell" forIndexPath:indexPath];
    
    return cell;
}


#pragma mark - empty data set

- (UIImage *)tableViewEmptyPlaceHolderImage
{
    return nil;
}

- (NSAttributedString *)tableViewEmptyTitle
{
    return nil;
}

- (NSAttributedString *)tableViewEmptyDescription
{
    return nil;
}

- (NSAttributedString *)tableViewEmptyButtonTitle
{
    return nil;
}

- (UIImage *)tableViewEmptyButtonBackgroundImage
{
    return nil;
}

- (UIView *)customEmptyView
{
    return nil;
}

- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView
{
    if ([self customEmptyView]) {
        return [self customEmptyView];
    } else {
        return nil;
    }
    
}

- (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state
{
    return [self tableViewEmptyButtonTitle];
}

- (UIImage *)buttonBackgroundImageForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state;
{
    return [self tableViewEmptyButtonBackgroundImage];
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView

{
    return [self tableViewEmptyTitle];
}

- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
{
    return [self tableViewEmptyDescription];
}

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
    return [self tableViewEmptyPlaceHolderImage];
}

- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view
{
    //    [self beginHeaderRefreshing];
}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView
{
    return YES;
}

- (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView
{
    return 64;
}

- (NSMutableArray *)dataArray
{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

//- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//    CGFloat sectionHeaderHeight = 80;
//    if (scrollView.contentOffset.y <= sectionHeaderHeight && scrollView.contentOffset.y >= 0) {
//        scrollView.contentInset = UIEdgeInsetsMake(- scrollView.contentOffset.y, 0, 0, 0);
//    } else if (scrollView.contentOffset.y >= sectionHeaderHeight) {
//        scrollView.contentInset = UIEdgeInsetsMake(- sectionHeaderHeight, 0, 0, 0);
//    }
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
